package com.cljh.controller;

import com.cljh.bean.UserAddress;
import com.cljh.controller.page.Code;
import com.cljh.controller.page.ProductPageBean;
import com.cljh.controller.page.Result;
import com.cljh.service.UserAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/address")
public class UserAddressController {
    @Autowired
    UserAddressService userAddressService;

    @GetMapping("/{id}")
    public Result selectById(@PathVariable Integer id) {
        UserAddress userAddress = userAddressService.selectById(id);
        Integer code=userAddress !=null ? Code.GET_OK:Code.GET_ERR;
        String msg=userAddress!=null ? "查询到信息":"未查询到信息";
        return new Result(code,userAddress,msg);

    }

    @GetMapping("/user/{id}")
    public Result selectByUserId(@PathVariable Integer id) {
        List<UserAddress> userAddresses = userAddressService.selectByUserId(id);
        Integer code=userAddresses !=null ? Code.GET_OK:Code.GET_ERR;
        String msg=userAddresses!=null ? "查询到信息":"未查询到信息";
        return new Result(code,userAddresses,msg);
    }


    @PostMapping
    public Result insert(@RequestBody UserAddress userAddress){
//        System.out.println(userAddress);
        boolean save = userAddressService.insertAdd(userAddress);
        Integer code=save ? Code.SAVE_OK:Code.SAVE_ERR;
        String msg=save ? "添加成功":"添加失败";
        return new Result(code,save,msg);
    }

    @PutMapping
    public Result update(@RequestBody UserAddress userAddress){
        boolean update = userAddressService.putAdd(userAddress);
        Integer code=update ? Code.UPDATE_OK:Code.UPDATE_ERR;
        String msg=update ? "修改成功":"修改失败";
        return new Result(code,update,msg);
    }


    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        boolean delete = userAddressService.delete(id);
        Integer code=delete ? Code.DELETE_OK:Code.DELETE_ERR;
        String msg=delete ? "删除一个成功":"删除一个失败";
        return new Result(code,delete,msg);
    }
}
